/**
 * @file icon-button
 * @author liaoxuezhi@baidu.com
 */
/* eslint fecs-indent: [0, "space", 2, 2] */

import * as React from 'react';
import {
  PureComponent,
  PropTypes
} from 'react';
import {Renderer} from '../../renderer';
import OnExpression from '../../hoc/on-expression';
import * as cx from 'classnames';

@Renderer({
  name: 'card-action',
  desc: '...',
  test: (path, config) => /(?:^|\/)card\-action$/.test(path)
})
@OnExpression
export default class CardAction extends PureComponent {
  static propTypes = {
    className: PropTypes.string,
    icon: PropTypes.string,
    actionType: PropTypes.oneOf(['ajax', 'dialog', 'link', 'url']),
    action: PropTypes.any,
    tooltip: PropTypes.string,
    onRequestAction: PropTypes.func
  };

  static defaultProps = {
    className: 'padder-v text-muted b-light'
  };

  constructor(props) {
    super(props);

    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    const {onRequestAction, item, data} = this.props;

    onRequestAction(this.props, item || data);
  }

  render() {
    const {
      className,
      icon,
      disabled,
      label,
    } = this.props;

    return (
      <a
        className={cx("col b-l", className, {
          disabled
        })}
        onClick={this.handleClick}
      >
        {icon ? (
          <i className={cx('block m-b-xs fa-2x', icon)} />
        ) : null}
        <span>{label}</span>
      </a>
    );
  }
}
